using System.ComponentModel.DataAnnotations;

namespace UniversalAdmin.Domain.Entities;

public class DocumentChunk : BaseEntity
{
    public Guid DocumentId { get; set; }

    [Required]
    public string Content { get; set; } = string.Empty;

    private float[]? _embedding;
    public float[]? Embedding
    {
        get => _embedding;
        set
        {
            if (value != null)
            {
                // 清理可能的无效值
                _embedding = value.Select(x =>
                {
                    if (float.IsNaN(x) || float.IsInfinity(x))
                        return 0.001f;
                    return Math.Abs(x) < 1e-10f ? 0.001f : x;
                }).ToArray();
            }
            else
            {
                _embedding = value;
            }
        }
    }

    // Navigation property
    public virtual Document Document { get; set; } = null!;
}